package com.bookmarket.controller;

import com.bookmarket.entity.Admin;
import com.bookmarket.entity.Role;
import com.bookmarket.service.HJHRoleService;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class HJHRoleController {

    @Resource
    private HJHRoleService roleService;

    @RequestMapping("/findAll")
    public Map findAll(int pageNum, int pageSize, Admin admin){
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("admin", admin);
        Page page =roleService.findAll(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }


    //删除管理员
    @RequestMapping("/deleteRole")
    public ModelAndView deleteRole(Role role){
        ModelAndView mv=new ModelAndView();
        roleService.deleteRole(role);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    @RequestMapping("/updateRole_Find")
    public ModelAndView updateRole_Find(int id){
        ModelAndView mv=new ModelAndView();
        List<Role> list=roleService.updateRole_Find();
        mv.addObject("role",list);
        mv.addObject("id",id);
        mv.setViewName("backstage/HJHUpdateRole");
        return mv;
    }

    @RequestMapping("/updateRole")
    public ModelAndView updateRole(int id,int rid){
        Role r=new Role();
        r.setId(rid);
        Map cond=new HashMap();
        cond.put("id",id);
        cond.put("role",r);
        ModelAndView mv=new ModelAndView();
        roleService.updateRole(cond);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }


    @RequestMapping("/ChangePass")
    public ModelAndView ChangePass(int id,String pass){
       ModelAndView mv=new ModelAndView();
       roleService.ChangePass(id,pass);
        System.out.println(id+"::"+pass);
       return mv;
    }


    public HJHRoleService getRoleService() {
        return roleService;
    }

    public void setRoleService(HJHRoleService roleService) {
        this.roleService = roleService;
    }
}